<?
class ArchiveController extends AppController {

	var $name = 'Archive';
	var $uses = array('Purchase', 'Category', 'MonthTotal', 'YearTotal');

	function month($year, $month) {

		$date = "$year-$month-01";

		$dts = strtotime($date);

		$prev_ts = strtotime('-1 month', $dts);
		$next_ts = strtotime('+1 month', $dts);

		$this->set('prev', $prev_ts);
		$this->set('next', $next_ts);
		$this->set('date', $dts);

		$this->set('navLinkTxt', 'F Y');
		$this->set('navLinkUrl', 'Y/m');

		$this->set('period', 'month');

		// CATEGORIES


		$cat_totals = $this->MonthTotal->find('all',
			array(
				'conditions' => array('date'=>$date, 'MonthTotal.amount > 0'),
				'order'=>'MonthTotal.amount DESC'
			)
		);

		$this->set('categories', $cat_totals);


		// PURCHASES

		$purchases = $this->Purchase->find('all',
			array(
				'order' => 'Purchase.date ASC',
				'conditions'=>array(
					"DATE_FORMAT( Purchase.date , '%Y-%m' )" => $year.'-'.$month
				)
			)
		);

		$this->set('purchases', $purchases);


		$this->render('archive');
	}

	function year($year) {

		$date = $year.'-01-01';

		$data = $this->YearTotal->find('all',
			array(
				'conditions' => array('date'=>$date, 'YearTotal.amount > 0'),
				'order'=>'YearTotal.amount DESC'
			)
		);

		$this->set('period', 'year');
		$this->set('date', strtotime($date));

		$this->render('archive');
	}

}
?>